package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.ui.booking.BookingRoomInfoActivity;
import com.wbdgj.utils.SpKeyUtils;

import java.text.DecimalFormat;
import java.util.List;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class RoomHourAdapter extends BaseListAdapter<LinkedTreeMap> {

    public RoomHourAdapter(Context context, List<LinkedTreeMap> objects) {
        super(context, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
                holder.noData = (TextView) convertView.findViewById(R.id.noData);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_booking_room_hour_item, parent, false);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.surplus = (TextView) convertView.findViewById(R.id.surplus);
                holder.hour_price = (TextView) convertView.findViewById(R.id.hour_price);
                holder.ydTxt = (TextView) convertView.findViewById(R.id.ydTxt);
                holder.mfTxt = (TextView) convertView.findViewById(R.id.mfTxt);
                holder.mfydTxt = (TextView) convertView.findViewById(R.id.mfydTxt);
                holder.fjxxLay = (LinearLayout) convertView.findViewById(R.id.fjxxLay);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(),getScreenHeight()*2/3);
            holder.noDataRootLayout.setLayoutParams(lp);
            holder.noData.setText("本店暂无可预订房间");
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            holder.name.setText(mDataList.get(position).get("name") + "（" + decimalFormat.format(mDataList.get(position).get("computer_number")) + "台电脑）");
            holder.hour_price.setText(mDataList.get(position).get("hour_price") +"");
            holder.surplus.setText("仅剩"+ decimalFormat.format(mDataList.get(position).get("surplus")) +"间");
            if(decimalFormat.format(mDataList.get(position).get("surplus")).equals("0")){
                holder.mfTxt.setVisibility(View.VISIBLE);
                holder.mfydTxt.setVisibility(View.VISIBLE);
                holder.ydTxt.setVisibility(View.GONE);
                holder.surplus.setVisibility(View.GONE);
            }else{
                holder.mfTxt.setVisibility(View.GONE);
                holder.mfydTxt.setVisibility(View.GONE);
                holder.ydTxt.setVisibility(View.VISIBLE);
                holder.surplus.setVisibility(View.VISIBLE);
            }
            holder.fjxxLay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mContext.startActivity(new Intent(mContext, BookingRoomInfoActivity.class).putExtra("id", decimalFormat.format(mDataList.get(position).get("id")))
                            .putExtra("IS_HOUR", "1")
                            .putExtra("surplus", decimalFormat.format(mDataList.get(position).get("surplus")))
                            .putExtra("startDateStr", BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.startDateStr))
                            .putExtra("endDateStr", BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.endDateStr)));
                }
            });
        }
        return convertView;
    }

    private static final class ViewHolder {
        TextView name, surplus, hour_price, ydTxt, mfTxt;
        TextView mfydTxt;
        LinearLayout noDataRootLayout;
        TextView noData;
        LinearLayout fjxxLay;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

}
